What is p-throttle?
The p-throttle npm package is used to throttle the execution of asynchronous functions. It allows you to limit the number of times a function can be called over a specified period, which is useful for rate-limiting API requests or controlling the flow of operations in a system.
What are p-throttle's main functionalities?
Basic Throttling
This feature allows you to throttle the execution of a function. In this example, the function can only be called twice per second.
const pThrottle = require('p-throttle');
const throttle = pThrottle({ limit: 2, interval: 1000 });
const throttled = throttle(async (index) => {
console.log(index);
});
(async () => {
for (let i = 0; i < 10; i++) {
throttled(i);
}
})();
Throttling with Promises
This feature demonstrates throttling with asynchronous functions that return promises. The function is throttled to execute once every two seconds.
const pThrottle = require('p-throttle');
const throttle = pThrottle({ limit: 1, interval: 2000 });
const throttled = throttle(async (index) => {
return new Promise((resolve) => {
setTimeout(() => {
console.log(index);
resolve();
}, 1000);
});
});
(async () => {
for (let i = 0; i < 5; i++) {
await throttled(i);
}
})();
Other packages similar to p-throttle
bottleneck
Bottleneck is a powerful rate limiter that can be used to control the rate of asynchronous operations. It offers more advanced features like clustering and priority queues, making it more suitable for complex scenarios compared to p-throttle.
limiter
Limiter is another rate-limiting library that provides a simple way to limit the number of operations over a given time period. It is similar to p-throttle but offers a more straightforward API for basic use cases.
rate-limiter-flexible
Rate-limiter-flexible is a highly flexible rate-limiting library that supports various backends like Redis and MongoDB. It provides more configuration options and is suitable for distributed systems, offering more flexibility than p-throttle.
p-throttle ![Build Status](https://travis-ci.org/sindresorhus/p-throttle.svg?branch=master)
Throttle promise-returning & async functions
It also works with normal functions.
Useful for rate limiting calls to an external API, for example.
Install
$ npm install --save p-throttle
Usage
Here, the trottled function is only called twice a second:
const pThrottle = require('p-throttle');
const now = Date.now();
const throttled = pThrottle(i => {
const secDiff = ((Date.now() - now) / 1000).toFixed();
return Promise.resolve(`${i}: ${secDiff}s`);
}, 2, 1000);
for (let i = 1; i <= 6; i++) {
throttled(i).then(console.log);
}
API
pThrottle(fn, limit, interval)
Returns a throttled version of fn
.
fn
Type: Function
Promise-returning/async function or a normal function.
limit
Type: number
Maximum number of calls within an interval
.
interval
Type: number
Timespan for limit
.
throttledFn.abort()
Abort pending executions. All unresolved promises are rejected with a pThrottle.AbortError
error.
Related
- p-debounce - Debounce promise-returning & async functions
- p-limit - Run multiple promise-returning & async functions with limited concurrency
- p-memoize - Memoize promise-returning & async functions
- More…
License
MIT © Sindre Sorhus